$(function(){
    loadTable();
})
function loadTable(){
    $.ajax({
        url:'/webapi/teacher/list'
    }).done(function (rs){

        let len = rs.length;
        let html = "";
        for(let i=0;i<len;i++)
        {
            let item = rs[i];
            let sex = null;
            item.sex == 1 ? sex = "男" : (item.sex == 2 ? sex = "女" : sex = "未知");
            html += "<tr>"
                + "<td>" + item.id + "</td>"
                + "<td>" + item.name + "</td>"
                + "<td>" + sex + "</td>"
                + "<td>" + item.subject + "</td>"
                + "<td> <a href='#' onclick='editStudent("+item.id+");'>编辑</a></td>"
                +"</tr>"
        }
        $("#teacherTb").html(html);
    })
}

function addStudent(){

    let data = $("#formTeacher").serialize();

    $.ajax({
        url: "/webapi/teacher/insert",
        method: "post",
        data: data
    }).done(function (){
        loadTable();
        $("#myModal").modal('hide');
    })
}

function editStudent(id){

    $('#myModal').modal('show');

    $.ajax({
        url: '/webapi/teacher/get/'+id
    }).done(function (rs){
        $("#id").val(rs.id);
        $("#name").val(rs.name);
        $("#password").val("");
        $("#sex").val(rs.sex);
        $("#subject").val(rs.subject);
    })
}